<!-- 售后列表 -->
<template>
  <s-layout title="我的售后">
    <!-- tab -->
    <su-sticky bgColor="#fff">
      <my-tabs
        :list="tabMaps"
        :scrollable="false"
        @change="onTabsChange"
        :current="state.currentTab"
      ></my-tabs>
    </su-sticky>
    <s-empty v-if="state.pagination.total === 0" icon="/static/data-empty.png" text="暂无数据">
    </s-empty>
    <!-- 列表 -->
    <view v-if="state.pagination.total > 0">
			<view
				v-for="order in state.pagination.data"
				:key="order.id"
				class="list-card ss-m-y-20">
				<view class="list-top ss-flex ss-row-between ss-col-center">
					<view class="top-left ss-flex ss-row-between ss-col-center">
						<img class="top-logo" src="../../../assets/logo.png" /></img>
						<text>九玄数字</text>
					</view>
					<view class="top-right"><text class="warning-color">{{order.aftersale_status_text}}</text></view>
				</view>
				<!-- 车辆信息 -->
				<view class="ss-flex ss-col-center">
					<image class="goods-img" :src="sheep.$url.cdn(order.goods_image)" mode="aspectFill"></image>
					<view class="goods-info">
						<view class="goods-name">{{order.goods_title}}</view>
						<view class="goods-color">{{order.goods_sku_text}}</view>
						<view class="goods-price">￥{{order.goods_price}}</view>
					</view>
				</view>
				<view class="actualPay ss-flex ss-col-center ss-row-right">实付款<text class="actualPay-price">￥89888.00</text></view>
				<view class="ss-flex ss-col-center ss-row-right">
					<view class="foot-btn" @tap.stop="onDelete(order.id)" v-if="order.btns.includes('delete')">
						删除记录
					</view>
					<view @tap.stop="onApply(order.id)" v-if="order.btns.includes('cancel')" class="foot-btn">
						撤销申请
					</view>
					<view class="foot-btn highlight-btn">
						修改申请
					</view>
				</view>
			</view>
    </view>
    <uni-load-more
      v-if="state.pagination.total > 0"
      :status="state.loadStatus"
      :content-text="{
        contentdown: '上拉加载更多',
      }"
      @tap="loadmore"
    />
  </s-layout>
</template>

<script setup>
  import sheep from '@/sheep';
  import { onLoad, onReachBottom } from '@dcloudio/uni-app';
  import { computed, reactive } from 'vue';
  import _ from 'lodash';

  const pagination = {
    data: [],
    current_page: 1,
    total: 1,
    last_page: 1,
  };
  const state = reactive({
    currentTab: 0,
    showApply: false,
    pagination: {
      data: [],
      current_page: 1,
      total: 1,
      last_page: 1,
    },
    loadStatus: '',
  });

  const tabMaps = [
    {
      name: '全部',
      value: 'all',
    },
    {
      name: '申请中',
      value: 'nooper',
    },
    {
      name: '处理中',
      value: 'ing',
    },
    {
      name: '已完成',
      value: 'completed',
    },
    {
      name: '已拒绝',
      value: 'refuse',
    },
  ];
  // 切换选项卡
  function onTabsChange(e) {
    state.pagination = pagination
    state.currentTab = e.index;
    getOrderList();
  }

  // 获取售后列表
  async function getOrderList(page = 1, list_rows = 5) {
    state.loadStatus = 'loading';
    let res = await sheep.$api.order.aftersale.list({
      type: tabMaps[state.currentTab].value,
      list_rows,
      page,
    });
    if (res.code === 1) {
      let orderList = _.concat(state.pagination.data, res.data.data);
			
      state.pagination = {
        ...res.data,
        data: orderList,
      };
			state.pagination.total=1
      if (state.pagination.current_page < state.pagination.last_page) {
        state.loadStatus = 'more';
      } else {
        state.loadStatus = 'noMore';
      }
    }
  }

  function onApply(orderId) {
    uni.showModal({
      title: '提示',
      content: '确定要取消此申请吗？',
      success: async function (res) {
        if (res.confirm) {
          const { code } = await sheep.$api.order.aftersale.cancel(orderId);
          if (code === 1) {
            state.pagination = pagination
            getOrderList();
          }
        }
      },
    });
  }

  function onDelete(orderId) {
    uni.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: async function (res) {
        if (res.confirm) {
          const { code } = await sheep.$api.order.aftersale.delete(orderId);
          if (code === 1) {
            state.pagination = pagination
            getOrderList();
          }
        }
      },
    });
  }

  onLoad(async (options) => {
    if (options.type) {
      state.currentTab = options.type;
    }
    getOrderList();
  });

  // 加载更多
  function loadmore() {
    if (state.loadStatus !== 'noMore') {
      getOrderList(state.pagination.current_page + 1);
    }
  }

  // 上拉加载更多
  onReachBottom(() => {
    loadmore();
  });
</script>

<style lang="scss" scoped>
	// 列表卡片样式
	.list-card{
		height: 465rpx;
		border-radius: 20rpx;
		background-color: #1E1E1E;
		margin: 11rpx 30rpx;
		padding: 0 30rpx
	}
	.list-top{
		padding: 35rpx 0 31rpx
	}
	.top-logo {
		margin-right: 24rpx;
		width: 42rpx;
		height: 42rpx;
	}
	.top-left{
		font-size: 28rpx;
		color: #EEEEEE;
	}
	.top-right{
		font-size: 28rpx;
	}
	.primary-color{
		color:#00A1FF
	}
	.danger-color{
		color:#C94F4F
	}
	.success-color{
		color:#13C093
	}
	.warning-color{
		color:#D39431
	}
	.goods-img{
		margin-right: 28rpx;
		border-radius:20rpx;
		width: 150rpx;
		height: 150rpx;
		overflow: hidden;
	}
	.goods-info{
		font-size: 32rpx;
		color: #E4E4E4;
		width: 320rpx;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}
	.goods-color{
		font-size: 22rpx;
		color: #868686;
		margin: 22rpx 0
	}
	.goods-price{
		color: #00A1FF;
	}
	
	.actualPay{
		color: #999999;
		font-size: 24rpx;
		margin: 60rpx 0 28rpx
	}
	.actualPay-price{
		color: #00A1FF;
		font-size: 34rpx;
	}
	.foot-btn{
		font-size: 24rpx;
		color: #999999;
		width:150rpx;
		height:54rpx;
		line-height:54rpx;
		text-align:center;
		border: 1rpx solid #C1C1C1;
		border-radius:27rpx;
		margin-left:20rpx
	}
	
	.highlight-btn{
		color: #151515;
		background-color: #45C9F0;
	}
	
</style>
